<?php

namespace app\home\controller;

use app\home\controller\Common;
use think\Db;
use think\facade\Env;

class Chart extends Common
{
   public function post(){
//       8981 2a07 501b1dfa000089
//       8981 9206 ab14289001009a  1682
       $data = input('post.');
       $data['createtime'] = time();
       $info = db('box')->where('port=' . $data['port'])->find();
       unset($data['port']);
       $data['box']=$info['box'];
       $data['boxid']=$info['id'];
       $r = db('chart')->insertGetId($data);
       if ($r) {
           $json['status'] = 1;
           $json['message'] = lang('success');
           /*********************记录日志***********************/
           addLog(session('user_name') . '添加记录-' . $data['title'] . ' [ ID:' . $r . ' ]');
           echo json_encode($json);
           exit;
       } else {
           $json['status'] = 0;
           $json['message'] =lang('fail');
           echo json_encode($json);
           exit;
       }

   }

    public function post_hongwai(){
        $data = input('post.');
        $number = substr($data['data'],4,4);
        $temp = '';
        for ($i = 0; $i < strlen($number);) {
            $temp .= strrev((substr($number, $i, 2)));
            $i += 2;
        }
        list($msec, $sec) = explode(' ', microtime());
        $data['createtime']  = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $data['number'] = base_convert(strrev($temp),16,10);
        $r = db('chart_hongwai')->insertGetId($data);
        if ($r) {
            $json['status'] = 1;
            $json['message'] = lang('success');
            /*********************记录日志***********************/
            addLog(session('user_name') . '添加记录-' . $data['title'] . ' [ ID:' . $r . ' ]');
            echo json_encode($json);
            exit;
        } else {
            $json['status'] = 0;
            $json['message'] =lang('fail');
            echo json_encode($json);
            exit;
        }

    }

    public function post_gls(){
        $data = input('post.');
//        if (strlen($data['data']) > 88) {
//            return;
//        }
//        list($msec, $sec) = explode(' ', microtime());
//        $data['createtime'] = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
//        $arr = array();
//        $ms = 0;
//        for ($i = 6; $i <= 88;) {
//            $number = substr($data['data'], $i, 6);
//            $arr[] =[
//                'number' => base_convert($number, 16, 10)-2000,
//                'data' => $number,
//                'createtime' => $data['createtime'] + $ms,
//            ];
//            $ms += 2;
//            $i += 8;
//        }
//        unset($arr[10]);
//        $r = db('chart_gls')->insertAll($arr);
        $number = substr($data['data'],6,6);
        list($msec, $sec) = explode(' ', microtime());
        $data['createtime']  = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $data['number'] = base_convert($number,16,10);
        $r = db('chart_gls')->insertGetId($data);
        if ($r) {
            $json['status'] = 1;
            $json['message'] = lang('success');
            /*********************记录日志***********************/
            addLog(session('user_name') . '添加记录-' . $data['title'] . ' [ ID:' . $r . ' ]');
            echo json_encode($json);
            exit;
        } else {
            $json['status'] = 0;
            $json['message'] =lang('fail');
            echo json_encode($json);
            exit;
        }

    }

}
